﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using DevComponents.DotNetBar;
using DevComponents.DotNetBar.Rendering;

namespace BWClient
{
    public partial class UserLoginDialog : DevComponents.DotNetBar.Office2007Form
    {

        public UserLoginDialog()
        {
            InitializeComponent();
        }

        private void UserLoginDialog_Load(object sender, EventArgs e)
        {
            BWClientData oBWClientData = BWClientDataManager.getCurrentBWClientData();
            if (oBWClientData.AutoLogin)
            {
                chkSavePwd.Checked = true;

                txtUserName.Text = oBWClientData.UserName;
                txtPwd.Text = CommonHelper.DESDecode(oBWClientData.Password, BWClientDataManager.DesKey);
            }
            else
            {
                chkSavePwd.Checked = false;
            }
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtUserName.Text.Trim()) || string.IsNullOrEmpty(txtPwd.Text.Trim()))
            {
                MessageBoxEx.Show("请输入帐户或密码！");
                return;
            }

            string strUserName = txtUserName.Text.Trim();
            string strPwd = txtPwd.Text.Trim();
            int iUserID = 0;

            switch (CommonHelper.UserLogin(strUserName, strPwd, chkSavePwd.Checked,ref iUserID))
            {
                case CommonHelper.UserLoginResult.LogonOK:

                    MainForm oMainForm = new MainForm();

                    BWClientData oBWClientData = BWClientDataManager.getCurrentBWClientData();
                    oBWClientData.UserName = strUserName;
                    oBWClientData.Password = strPwd;
                    oBWClientData.iUserID = iUserID;

                    oMainForm.m_BWClientData = oBWClientData;

                    this.Hide();

                    oMainForm.ShowDialog();

                    Application.ExitThread();
                    break;
                case CommonHelper.UserLoginResult.UserPwdError:
                    txtUserName.Focus();
                    break;
                case CommonHelper.UserLoginResult.WebServiceError:
                    Application.ExitThread();
                    break;
            }
        }
        private void txtPwd_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == (char)13)
            {
                buttonX1_Click(sender, e);
            }
        }

        private void txtUserName_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == (char)13)
            {
                txtPwd.Focus();
            }
        }
    }
}
